package geradatawarehouse.dao.oracle;

import geradatawarehouse.bean.Aluno;
import geradatawarehouse.dao.abs.AbstractDAO;
import geradatawarehouse.dao.oracle.abs.OracleDAO;

import java.io.IOException;
import java.sql.SQLException;

public class AlunoOracleDAO extends OracleDAO {

	private static final String ALUNO_SQL = "aluno.sql";

	public AlunoOracleDAO() throws IOException {
		super();
	}

	public void executeSelectAll() throws SQLException, IOException {
		this.resultSet = this.querySelect(AbstractDAO
				.loadQueryFile(AlunoOracleDAO.ALUNO_SQL));
	}

	public Aluno getNext() throws SQLException {
		if (this.resultSet != null && this.resultSet.next()) {
			final String dtNasc = this.resultSet.getString("DTNASCIMENTO");
			final String sexo = this.resultSet.getString("SEXO");
			String cep = this.resultSet.getString("CEP");

			Integer cepInt = null;

			if (cep != null) {
				cep = cep.replace("-", "");

				try {
					cepInt = Integer.parseInt(cep);
				} catch (final NumberFormatException e) {
					// Se der erro, fica null mesmo
				}
			}

			return new Aluno(this.resultSet.getString("CODIGO"),
					this.resultSet.getString("NOME"),
					dtNasc != null ? this.parseDate(dtNasc) : null,
					this.resultSet.getString("NOMEMAE"), cepInt,
					this.resultSet.getString("TELEFONEMAE"),
					this.resultSet.getString("LOGRADOURO"),
					this.resultSet.getString("ENDERECO"),
					this.resultSet.getString("BAIRRO"),
					this.resultSet.getString("CIDADE"),
					this.resultSet.getString("UF"), sexo != null ? sexo
							.toLowerCase().equals("m") : null);
		}

		return null;
	}
}
